package yxy.game.pm2.command.hero;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.clout;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * 热门武将阵容推荐
 */
@MessageType(proto = Command.HeroRecommend,classtype = HeroMessage.HeroRecommendRequest.class)
public class CMD_HeroRecommend extends Command<HeroMessage.HeroRecommendRequest> {


    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroRecommendRequest request) throws Exception {
        HeroMessage.HeroRecommendResponse.Builder response = HeroMessage.HeroRecommendResponse.newBuilder();

        //热门推荐返回
        HashMap<String, String> hotList = getHotList();

        response.setHotHero(hotList.get("hotHeroArrary"));
        response.setHotHeroTeam(hotList.get("hotHeroTeamArrary"));

        send(messageSource,response, Result.Success);
    }

    private void send(MessageSource messageSource, HeroMessage.HeroRecommendResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    //热门返回
    private HashMap<String, String> getHotList() {
        HashMap<String, String> returnMap = new HashMap<>();
        Map<String, clout> allData = DataCenter.getAllData(clout.class);
        JSONArray hotHeroArrary = new JSONArray();
        JSONArray hotHeroTeamArrary = new JSONArray();
        for (yxy.cherry.data.bean.clout clout : allData.values()) {
            if (clout.getType() == 1) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", clout.getType());
                jsonObject.put("heroId", clout.getHeroid()[0]);
                jsonObject.put("heroHeat", clout.getClout());
                jsonObject.put("detail", clout.getDetail());
                jsonObject.put("id", clout.getId());
                hotHeroArrary.add(jsonObject);

            }

            if (clout.getType() == 2) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("type", clout.getType());
                jsonObject.put("heroId", clout.getHeroid());
                jsonObject.put("heroHeat", clout.getClout());
                jsonObject.put("detail", clout.getDetail());
                jsonObject.put("id", clout.getId());
                hotHeroTeamArrary.add(jsonObject);
            }

        }


        Collections.sort(hotHeroArrary, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                JSONObject json1 = (JSONObject) o1;
                JSONObject json2 = (JSONObject) o2;
                Integer heroHeat1 = json1.getInteger("heroHeat");
                Integer heroHeat2 = json2.getInteger("heroHeat");
                return heroHeat2 - heroHeat1;
            }
        });


        Collections.sort(hotHeroTeamArrary, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                JSONObject json1 = (JSONObject) o1;
                JSONObject json2 = (JSONObject) o2;
                Integer heroHeat1 = json1.getInteger("heroHeat");
                Integer heroHeat2 = json2.getInteger("heroHeat");
                return heroHeat2 - heroHeat1;
            }
        });


        returnMap.put("hotHeroArrary", hotHeroArrary.toString());
        returnMap.put("hotHeroTeamArrary", hotHeroTeamArrary.toString());
        return returnMap;
    }
}
